﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Meqs.Entity;
using Meqs.Logic.Interface;
using System.Collections.Generic;
using Castle.Windsor;

[Serializable]
public class CauseState
{
    public CauseState()
    {
    }

    private int _rID;
    private int _causeNo;
    private string _causeName;
    private int _state;

    public int RID
    {
        get { return _rID; }
        set { _rID = value; }
    }

    public int CauseNo
    {
        get { return _causeNo; }
        set { _causeNo = value; }
    }

    public string CauseName
    {
        get { return _causeName; }
        set { _causeName = value; }
    }

    public int State
    {
        get { return _state; }
        set { _state = value; }
    }
};

public partial class School_CauseSetting : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    [System.Web.Services.WebMethod]
    public static List<CauseState> InitSchoolCause()
    {
        //获取所有课程
        Hashtable htParam = new Hashtable();
        htParam.Add("IsEffect", 1);
        List<GlobalCauseInfo> globalCause = (List<GlobalCauseInfo>)Container<IGlobalCauseInfoLogic>.Instance.FindAll(htParam);

        Hashtable ht = new Hashtable();
        //根据学校标识，查询学校的课程ID
        ht.Clear();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        List<CauseInfo> SchoolCause = (List<CauseInfo>)Container<ICauseInfoLogic>.Instance.FindAll(ht);

        //比较前两者的数据，生成一个状态赋给成员变量保存
        List<CauseState> causeState = new List<CauseState>();

        //将全部课程复制到自定义课程链表里面去，State全为False
        for (int i = 0; i < globalCause.Count; i++)
        {
            CauseState cause = new CauseState();
            cause.RID = globalCause[i].RID;
            cause.CauseNo = globalCause[i].CauseNo;
            cause.CauseName = globalCause[i].CauseName;
            cause.State = 0;
            causeState.Add(cause);
        }

        //判断学校里面是否选择了这门课程ID，如果是，则将标识改为1
        for (int j = 0; j < SchoolCause.Count; j++)
        {
            int RID = SchoolCause[j].RID;
            for (int k = 0; k < causeState.Count; k++)
            {
                if (RID == causeState[k].RID)
                {
                    causeState[k].State = 1;
                }
            }
        }
        //返回给前台
        return causeState;
    }

    [System.Web.Services.WebMethod]
    public static int SaveCauseSetting(List<int> choose, List<int> unchoose)
    {
        //判断unchoose里面的课程，是否都有老师在教，如果有，则返回-1
        for (int i = 0; i < unchoose.Count; i++)
        {
            int iCount = Container<ICauseInfoLogic>.Instance.FindCauseUserCount(unchoose[i], Common.GetConstSchoolMark());
            if (iCount > 0)
                return -1;
        }
        
        //获取初始状态
        List<CauseState> SchoolCauseState = new List<CauseState>();
        SchoolCauseState = InitSchoolCause();

        //判断choose里面，是否有原来没有选择的课程，如果有，则添加
        //找出原来不在的课程ID 放入到 List<int> Adds

        for (int j = 0; j < SchoolCauseState.Count; j++)
        {
            CauseState cs = SchoolCauseState[j];

            if(cs.State == 1)
            {
                for(int k = 0; k < choose.Count; k++)
                {
                    if (cs.RID == choose[k])
                        choose.Remove(cs.RID);
                }
            }
            else
            {
                for(int g = 0; g<unchoose.Count; g++)
                {                    
                    if (cs.RID == unchoose[g])
                        unchoose.Remove(cs.RID);
                }
            }
        }

        if (choose.Count == 0 && unchoose.Count == 0)
            return -2;
    
        
        //调用 函数，将choose里面的添加到数据库中，将unchoose里面的删除

        int iExcute = Container<ICauseInfoLogic>.Instance.ModifyCause(choose, unchoose, Common.GetConstSchoolMark());

        return iExcute;
    }

}
